package com.fz.study.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.study.project.entity.Course;
import com.fz.study.project.entity.qurey.CourseQuery;
import com.fz.study.project.exception.BizException;
import com.fz.study.project.mapper.CourseMapper;
import com.fz.study.project.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程信息 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-02-17
 */
@Slf4j
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Override
    public void saveByName(Course course) {
//        判断传递的数据是否为空
        if (Objects.isNull(course)){
            BizException.throwBizException("传递的数据不能为空！");
        }
//        判断添加的数据是否在数据库中存在
        Course oldUser = lambdaQuery().eq(Course::getCourseName, course.getCourseName()).one();
        if (!Objects.isNull(oldUser)){
            BizException.throwBizException("该用户在系统中已经存在，不用重复添加！");
        }
/*        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());*/
        save(course);
    }

    @Transactional
    @Override
    public void removeWithIds(List<Integer> ids) {
        removeByIds(ids);
    }

    @Transactional
    @Override
    public void updateWithId(Course course) {
//        判断course数据是否为空
        if (Objects.isNull(course)){
            BizException.throwBizException("修改数据不能空！");
        }
//        判断数据是都与原始数据一致
        Course name = lambdaQuery().eq(Course::getCourseName, course.getCourseName()).one();
        Course CourseIntr = lambdaQuery().eq(Course::getCourseIntr, course.getCourseIntr()).one();
        if (!Objects.isNull(name) && !Objects.isNull(CourseIntr)){
            BizException.throwBizException("数据为原始数据，不需要修改！");
        }
//        判断修改的id是否存在
        Course count = lambdaQuery().eq(Course::getId, course.getId()).one();
        if (Objects.isNull(count)){
            BizException.throwBizException("要修改的用户Id不存在！请重新输入！");
        }
//        course.setUpdateTime(LocalDateTime.now());
        updateById(course);
    }

    @Override
    public Page<Course> findByPage(CourseQuery query) {
        Page<Course> page = new Page<>(query.getCurrent(), query.getSize());
        log.info("前端传递的数据为：{}",query);
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(query.getId() != null,Course::getId,query.getId())
                        .like(Course::getCourseName,query.getCourseName())
                                .like(Course::getCourseIntr,query.getCourseIntr());
        return page(page, queryWrapper);
    }

}
